﻿using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormsApp1
{
    /// <summary>
    /// 提取文字区域
    /// </summary>
    internal class ExtractTextAreaV2
    {
        public void findTextRegion(Mat dilation, Mat mat)
        {
            // 1. 查找轮廓
            OpenCvSharp.Point[][] contours;
            HierarchyIndex[] hierarchly;
            Rect biggestContourRect = new Rect();

            Cv2.FindContours(dilation, out contours, out hierarchly, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple);

            // 2. 筛选那些面积小的
            int i = 0;
            foreach (OpenCvSharp.Point[] contour in contours)
            {
                double area = Cv2.ContourArea(contour);

                //面积小的都筛选掉
                if (area < 5000)
                {
                    continue;
                }

                //轮廓近似，作用很小
                double epsilon = 0.001 * Cv2.ArcLength(contour, true);

                //找到最小的矩形
                biggestContourRect = Cv2.BoundingRect(contour);

                if (biggestContourRect.Height > (biggestContourRect.Width * 1.2))
                {
                    continue;
                }

                //画线
                mat.Rectangle(biggestContourRect, new Scalar(0, 255, 0), 2);
            }

            //pictureBox1.Image = mat.ToBitmap();
            Cv2.ImShow("img", mat);
        }
        public Mat preprocess(Mat src)
        {
            Mat dilation2 = new Mat();

            //1.S高斯滤波
            Mat gs = new Mat();
            Cv2.GaussianBlur(src, gs, new OpenCvSharp.Size(5, 5), 10.1, 11.0);
            //对图片进行canny边缘检测
            Mat canny = new Mat();
            Cv2.Canny(gs, canny, 50, 200);
            Cv2.ImShow("Form15", canny);

            //2.二值化
            Mat binary = new Mat();
            Cv2.Threshold(gs, binary, 0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary);

            //3. 膨胀和腐蚀操作的核函数
            Mat element1 = new Mat();
            Mat element2 = new Mat();
            OpenCvSharp.Size size1 = new OpenCvSharp.Size(30, 9);
            OpenCvSharp.Size size2 = new OpenCvSharp.Size(24, 6);

            element1 = Cv2.GetStructuringElement(MorphShapes.Rect, size1);
            element2 = Cv2.GetStructuringElement(MorphShapes.Rect, size2);

            //4. 膨胀一次，让轮廓突出
            Mat dilation = new Mat();
            Cv2.Dilate(binary, dilation, element2);

            //5. 腐蚀一次，去掉细节，如表格线等。注意这里去掉的是竖直的线
            Mat erosion = new Mat();
            Cv2.Erode(dilation, erosion, element1);

            //6. 再次膨胀，让轮廓明显一些
            Cv2.Dilate(erosion, dilation2, element2, null, 3);
            return dilation2;
        }
    }
}
